package com.software.course.controller;

import com.software.course.enity.Course;
import com.software.course.enity.User;
import com.software.course.service.AdminService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/adminRole")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/editStudentInfo.html")
    public String editStudent(Model model){
        List<User> stus = adminService.listStu();
        model.addAttribute("stus",stus);
        return "tables/editStudentInfo";
    }

    @GetMapping("/deleteStudent/{id}")
    public String deleteStu(@PathVariable("id") Integer id, RedirectAttributes attributes){
        boolean b = adminService.deleteStuById(id);
        if(b){
            attributes.addFlashAttribute("message","删除成功");
            return "redirect:/adminRole/editStudentInfo.html";
        }else {
            attributes.addFlashAttribute("message","删除失败");
            return "redirect:/adminRole/editStudentInfo.html";
        }
    }
     @GetMapping("/toeditStudent/{id}")
        public String toeditStu(@PathVariable Integer id,Model model){
            model.addAttribute("student",adminService.queryStuById(id));
            return "edits/editStudent";
        }

    @PostMapping("/editStudent")
      public String editStu(User user,RedirectAttributes attributes){
            boolean b = adminService.updateStu(user);
            if(b){
                attributes.addFlashAttribute("message","更新信息成功");
                return "redirect:/adminRole/editStudentInfo.html";
            }
            else {
                attributes.addFlashAttribute("message","更新信息失败,账户已存在");
                return "edits/editStudent";
            }
        }
    @GetMapping("/toUpdateStu")
    public String toUpdateStu(Model model){
        User stu = new User();
        model.addAttribute("student",stu);
        return "edits/updateStudent";
    }

    @PostMapping("/updateStudent")
    public String updateStudent(User user){
        boolean b = adminService.addStu(user);
        if(b){
            return "redirect:/adminRole/editStudentInfo.html";
        }
        else {
            return "edits/editStudent";
        }
    }

    @GetMapping("/editCourseInfo.html")
    public String editCourse(Model model){
        List<Course> courseEdits = adminService.listCourse();
        model.addAttribute("courseEdits",courseEdits);
        return "tables/editCourseInfo";
    }

    @GetMapping("/deleteCourse/{id}")
    public String delCourse(@PathVariable("id") int id,RedirectAttributes attributes){
        boolean b = adminService.deleteCourseById(id);
        if(b){
            attributes.addFlashAttribute("message","删除成功");
            return "redirect:/adminRole/editCourseInfo.html";
        }else {
            attributes.addFlashAttribute("message","删除失败");
            return "redirect:/adminRole/editCourseInfo.html";
        }
    }

    @GetMapping("/toeditCourse/{id}")
    public String toeditCourse(@PathVariable("id") int id,Model model){
        Course course = adminService.queryCourseById(id);
        model.addAttribute("course",course);
        return "edits/editCourse";
    }

    @PostMapping("/editCourse")
    public String editCourse(Course course,RedirectAttributes attributes){
        adminService.updateCourse(course);
        return "redirect:/adminRole/editCourseInfo.html";
    }

    @GetMapping("/toUodateCourse")
    public String toUpdateCourse(Model model){
        User user = new User();
        model.addAttribute("course",user);
        return "edits/updateCourse";
    }

    @PostMapping("/updateCourse")
    public String updateCourse(Course course){
        adminService.addCourse(course);
        return "redirect:/adminRole/editCourseInfo.html";
    }

    @GetMapping("/editTeacherInfo.html")
    public String editTeacher(Model model){
        List<User> teachers = adminService.lisTeacher();
        model.addAttribute("teachers",teachers);
        return "tables/editTeacherInfo";
    }









}
